package com.newvpn.fastbest.zenith.utils;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.newvpn.fastbest.zenith.VPNApp;
import com.newvpn.fastbest.zenith.BuildConfig;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class DeviceUtils {

    public static String deviceID ;

    public static String getSystemlanguage() {
        Locale currentLocale = Locale.getDefault();
        String language = currentLocale.getLanguage(); // 获取语言代码，例如"en"表示英语
        return language;
    }

    public static String getApplanguage() {
        String language = getSystemlanguage();
        language  = KeyStore.INS.getString(KeyUtils.LANGUAGE, language);
        if (TextUtils.isEmpty(language)){
            language = "en";
        }
        return language;
    }

    public static String getDeviceId() {
        if (TextUtils.isEmpty(deviceID)){
            deviceID = KeyStore.INS.getString(KeyUtils.DEVICEID,"");
        }
        return deviceID;
    }

    public static String getVersion() {
       return BuildConfig.VERSION_CODE + "";
    }

    private static String generateDeviceId(Context context) {
        try {
            String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            String fixedInfo = getFixedDeviceInfo();
            String concatenated = androidId + fixedInfo;
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(concatenated.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String h = Integer.toHexString(0xff & b);
                while (h.length() < 2) h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString().substring(0, 32);
        } catch (NoSuchAlgorithmException e) {
            //Log.e(TAG, "Hashing algorithm not found", e);
        }
        return null;
    }

    private static String getFixedDeviceInfo() {
        return String.format(Locale.US, "%s%s%s%s%s",
                Build.MODEL, Build.DEVICE, Build.BOARD, Build.HARDWARE, Build.PRODUCT);
    }

    private static String adid;

    public static void makeAdid() {
        adid = getAdvertisingId(VPNApp.getBase());
    }

    public static boolean adidIsEmpty(){
        return TextUtils.isEmpty(adid);
    }

    private static String getAdvertisingId(Context context) {
        try {
            AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
            return adInfo != null ? adInfo.getId().replace("-", "") : null;
        } catch (IOException | IllegalStateException | NullPointerException |
                 GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
            //Log.e(TAG, "Failed to get advertising ID", e);
        }
        return null;
    }

    public static void checkAdid() {
        if (!TextUtils.isEmpty(adid)){
            KeyStore.INS.putString(KeyUtils.DEVICEID, adid);
        }else {
            KeyStore.INS.putString(KeyUtils.DEVICEID, generateDeviceId(VPNApp.getBase()));
        }
    }

    public static String getCountry() {
        TelephonyManager telephonyManager = (TelephonyManager) VPNApp.getBase().getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getSimCountryIso();
    }
}
